function [bout_dur,bout_lenS,bout_speedS,bout_lenRf,bout_speedRf,bout_lenLf,bout_speedLf, ...
    bout_lenRh,bout_speedRh,bout_lenLh,bout_speedLh] = getkin(MsRawData,labels,group_num,fps)

%GETKIN     Extracts kinematics from desired group number using raw data and labels.
%   
% [BOUT_DUR,BOUT_LENS,BOUT_SPEEDS,BOUT_LENRF,BOUT_SPEEDRF,BOUT_LENLF,BOUT_SPEEDLF, ...
%     BOUT_LENRH,BOUT_SPEEDRH,BOUT_LENLH,BOUT_SPEEDLH] = GETKIN(MSRAWDATA,LABELS,GROUP_NUM,FPS)
%
%   INPUTS:
%   MSRAWDATA    Matrix of the positons of the 6-body parts outlining the rodent over time videotaped from the bottom looking up. 
%                Rows represents time.
%                Columns 1 & 2 tracks snout; columns 3 to 6 tracks the two front paws; Columns 7 to 10 tracks the two hind paws;
%                Columns 11 & 12 tracks the base of the tail. Tested on tracking data generated by DeepLabCut 2.0.
%   LABELS    The label cell corresponding to the MsRawData cell.
%   GROUP_NUM    Desired group number for kinematics extraction.
%
%   OUTPUTS:
%   BOUT_DUR    Bout duration in frames, a cell that contains all bouts.
%   BOUT_LENS    Bout distances in pixels of the snout, a cell that contains all bouts.
%   BOUT_SPEEDS    Bout peak speed in pixels/frame of the snout, a cell that contains all bouts.
%   BOUT_LENRF    Bout distances in pixels of the left forepaw, a cell that contains all bouts.
%   BOUT_SPEEDRF    Bout peak speed in pixels/frame of the right forepaw, a cell that contains all bouts.
%   BOUT_LENLF    Bout distances in pixels of the left forepaw, a cell that contains all bouts.
%   BOUT_SPEEDLF    Bout peak speed in pixels/frame of the left forepaw, a cell that contains all bouts.
%   BOUT_LENRH    Bout distances in pixels of the right hind paw, a cell that contains all bouts.
%   BOUT_SPEEDRH    Bout peak speed in pixels/frame of the right hind paw, a cell that contains all bouts.
%   BOUT_LENLH    Bout distances in pixels of the left hind paw, a cell that contains all bouts.
%   BOUT_SPEEDLH    Bout peak speed in pixels/frame of the left hind paw, a cell that contains all bouts.
%   
%   Created by Alexander Hsu, Date: 031020
%   Contact ahsu2@andrew.cmu.edu

    smth_hstry = round(0.05/(1/fps))-1;
    smth_futr = round(0.05/(1/fps))-1;
    MshindRpos_perlocbout = {};
    for an = 1:length(labels)
        loc_idx{an} = find(labels{an} == group_num);
        endbout{an} = diff(loc_idx{an}) ~= 1;
        [B{an}, N{an}, BI{an}] = RunLength_M(endbout{an});
        MsRawData_smth{an} = movmean(MsRawData{an},[smth_hstry,smth_futr]);
        for loc_bout = 1:2:length(N{an})
            Mssntpos_perlocbout{an,((loc_bout+1)/2)} = MsRawData_smth{an}(loc_idx{an}(BI{an}(loc_bout):BI{an}(loc_bout)+N{an}(loc_bout)),1:2);
            MsforeRpos_perlocbout{an,((loc_bout+1)/2)} = MsRawData_smth{an}(loc_idx{an}(BI{an}(loc_bout):BI{an}(loc_bout)+N{an}(loc_bout)),3:4);
            MsforeLpos_perlocbout{an,((loc_bout+1)/2)} = MsRawData_smth{an}(loc_idx{an}(BI{an}(loc_bout):BI{an}(loc_bout)+N{an}(loc_bout)),5:6);
            MshindRpos_perlocbout{an,((loc_bout+1)/2)} = MsRawData_smth{an}(loc_idx{an}(BI{an}(loc_bout):BI{an}(loc_bout)+N{an}(loc_bout)),7:8);
            MshindLpos_perlocbout{an,((loc_bout+1)/2)} = MsRawData_smth{an}(loc_idx{an}(BI{an}(loc_bout):BI{an}(loc_bout)+N{an}(loc_bout)),9:10);
        end
        for n = 1:sum(~cellfun(@isempty,MshindRpos_perlocbout(an,:)),2)
            behvbout_dur{an}(n) = length(MshindRpos_perlocbout{an,n});
            for t = 2:length(MshindRpos_perlocbout{an,n})
                snoutdeltapos{an}(n,t-1) = norm([Mssntpos_perlocbout{an,n}(t,1:2) - Mssntpos_perlocbout{an,n}(t-1,1:2)]);
                forelimbdeltaposR{an}(n,t-1) = norm([MsforeRpos_perlocbout{an,n}(t,1:2) - MsforeRpos_perlocbout{an,n}(t-1,1:2)]);
                forelimbdeltaposL{an}(n,t-1) = norm([MsforeLpos_perlocbout{an,n}(t,1:2) - MsforeLpos_perlocbout{an,n}(t-1,1:2)]);
                hindlimbdeltaposR{an}(n,t-1) = norm([MshindRpos_perlocbout{an,n}(t,1:2) - MshindRpos_perlocbout{an,n}(t-1,1:2)]);
                hindlimbdeltaposL{an}(n,t-1) = norm([MshindLpos_perlocbout{an,n}(t,1:2) - MshindLpos_perlocbout{an,n}(t-1,1:2)]);
            end
        end
        for nn = 1:length(snoutdeltapos{an}(:,1))
            if ~isempty(findpeaks(snoutdeltapos{an}(nn,1:round(fps/10):end)))
                [PKS_S, ~] = findpeaks(snoutdeltapos{an}(nn,1:round(fps/10):end));
                [~, LOCT_S] = findpeaks(-snoutdeltapos{an}(nn,1:round(fps/10):end));
                for strd = 1:length(LOCT_S)
                    if strd == 1
                        bout_lenS{an}{nn}(strd) = sum(snoutdeltapos{an}(nn,1:LOCT_S(strd)*round(fps/10)));
                    else
                        bout_lenS{an}{nn}(strd) = sum(snoutdeltapos{an}(nn,LOCT_S(strd-1)*round(fps/10):LOCT_S(strd)*round(fps/10)));
                    end
                end
                bout_speedS{an}{nn} = PKS_S;
                bout_dur{an}{nn} = length(nonzeros(snoutdeltapos{an}(nn,1:end)));
            elseif isempty(findpeaks(snoutdeltapos{an}(nn,1:round(fps/10):end)))
                bout_lenS{an}{nn} = 0;
                bout_speedS{an}{nn} = 0;
            end
        end
        for nn = 1:length(forelimbdeltaposR{an}(:,1))
            if ~isempty(findpeaks(forelimbdeltaposR{an}(nn,1:round(fps/10):end)))
                [PKS_Rf, ~] = findpeaks(forelimbdeltaposR{an}(nn,1:round(fps/10):end));
                [~, LOCT_Rf] = findpeaks(-forelimbdeltaposR{an}(nn,1:round(fps/10):end));
                for strd = 1:length(LOCT_Rf)
                    if strd == 1
                        bout_lenRf{an}{nn}(strd) = sum(forelimbdeltaposR{an}(nn,1:LOCT_Rf(strd)*round(fps/10)));
                    else
                        bout_lenRf{an}{nn}(strd) = sum(forelimbdeltaposR{an}(nn,LOCT_Rf(strd-1)*round(fps/10):LOCT_Rf(strd)*round(fps/10)));
                    end
                end
                bout_speedRf{an}{nn} = PKS_Rf;
            elseif isempty(findpeaks(forelimbdeltaposR{an}(nn,1:round(fps/10):end)))
                bout_lenRf{an}{nn} = 0;
                bout_speedRf{an}{nn} = 0;
            end
        end
        for nn = 1:length(forelimbdeltaposL{an}(:,1))
            if ~isempty(findpeaks(forelimbdeltaposL{an}(nn,1:round(fps/10):end)))
                [PKS_Lf, ~] = findpeaks(forelimbdeltaposL{an}(nn,1:round(fps/10):end));
                [~, LOCT_Lf] = findpeaks(-forelimbdeltaposL{an}(nn,1:round(fps/10):end));
                for strd = 1:length(LOCT_Lf)
                    if strd == 1
                        bout_lenLf{an}{nn}(strd) = sum(forelimbdeltaposL{an}(nn,1:LOCT_Lf(strd)*round(fps/10)));
                    else
                        bout_lenLf{an}{nn}(strd) = sum(forelimbdeltaposL{an}(nn,LOCT_Lf(strd-1)*round(fps/10):LOCT_Lf(strd)*round(fps/10)));
                    end
                end
                bout_speedLf{an}{nn} = PKS_Lf;
            elseif isempty(findpeaks(forelimbdeltaposL{an}(nn,1:round(fps/10):end)))
                bout_lenLf{an}{nn} = 0;
                bout_speedLf{an}{nn} = 0;
            end
        end
        for nn = 1:length(hindlimbdeltaposR{an}(:,1))
            if ~isempty(findpeaks(hindlimbdeltaposR{an}(nn,1:round(fps/10):end)))
                [PKS_Rh, ~] = findpeaks(hindlimbdeltaposR{an}(nn,1:round(fps/10):end));
                [~, LOCT_Rh] = findpeaks(-hindlimbdeltaposR{an}(nn,1:round(fps/10):end));

                for strd = 1:length(LOCT_Rh)
                    if strd == 1
                        bout_lenRh{an}{nn}(strd) = sum(hindlimbdeltaposR{an}(nn,1:LOCT_Rh(strd)*round(fps/10)));
                    else
                        bout_lenRh{an}{nn}(strd) = sum(hindlimbdeltaposR{an}(nn,LOCT_Rh(strd-1)*round(fps/10):LOCT_Rh(strd)*round(fps/10)));
                    end
                end
                bout_speedRh{an}{nn} = PKS_Rh;
            elseif isempty(findpeaks(hindlimbdeltaposR{an}(nn,1:round(fps/10):end)))
                bout_lenRh{an}{nn} = 0;
                bout_speedRh{an}{nn} = 0;
            end
        end
        for nn = 1:length(hindlimbdeltaposL{an}(:,1))
            if ~isempty(findpeaks(hindlimbdeltaposL{an}(nn,1:round(fps/10):end)))
                [PKS_Lh, ~] = findpeaks(hindlimbdeltaposL{an}(nn,1:round(fps/10):end));
                [~, LOCT_Lh] = findpeaks(-hindlimbdeltaposL{an}(nn,1:round(fps/10):end));

                for strd = 1:length(LOCT_Lh)
                    if strd == 1
                        bout_lenLh{an}{nn}(strd) = sum(hindlimbdeltaposL{an}(nn,1:LOCT_Lh(strd)*round(fps/10)));
                    else
                        bout_lenLh{an}{nn}(strd) = sum(hindlimbdeltaposL{an}(nn,LOCT_Lh(strd-1)*round(fps/10):LOCT_Lh(strd)*round(fps/10)));
                    end
                end
                bout_speedLh{an}{nn} = PKS_Lh;
            elseif isempty(findpeaks(hindlimbdeltaposL{an}(nn,1:round(fps/10):end)))
                bout_lenLh{an}{nn} = 0;
                bout_speedLh{an}{nn} = 0;
            end
        end
    end
    
return

